package com.lujianfei.waterpower.ui.power;

import android.content.Context;
import android.os.Bundle;

import com.lujianfei.waterpower.R;
import com.lujianfei.waterpower.adapter.PowerAdapter;
import com.lujianfei.waterpower.base.BaseActivity;

import java.util.List;
import java.util.Objects;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class PowerActivity extends BaseActivity implements PowerContract.View {

    RecyclerView listview;
    PowerContract.Presenter presenter;
    PowerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_power);
        setTitleMiddleText("电费统计");
        presenter = new PowerPresenter(this);
        listview = findViewById(R.id.listview);
        initView();
        showTitleRightImage();
        setTitleRightImage(R.drawable.ic_add);
    }

    private void initView() {
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(Objects.requireNonNull(ContextCompat.getDrawable(this, R.drawable.divider)));
        listview.setLayoutManager(new LinearLayoutManager(this));
        listview.addItemDecoration(divider);
    }

    @Override
    public void onTitleRightImageClick() {
        super.onTitleRightImageClick();
        presenter.openPowerAdd();
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.loadData();
    }

    @Override
    protected int getTitleBarResId() {
        return R.id.custom_titlebar;
    }

    @Override
    public void updateData(List<PowerEntity> powerEntities) {
        if (null == adapter) {
            adapter = new PowerAdapter(presenter);
            listview.setAdapter(adapter);
        }
        adapter.setData(powerEntities);
    }

    @Override
    public Context getContext() {
        return this;
    }
}
